package web.ctroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.R;
import pojo.User;

import java.util.ArrayList;
import java.util.List;

//@Controller
//@ResponseBody  //作用于当前类中的所有控制器方法
@RestController  //相当于 @Controller + @ResponseBody
@RequestMapping("/rest")
public class RestFullController {

    /*
        http://localhost:80/rest/1
        请求方式为Get表示获取，结合路径，表示根据id获取用户
     */
//    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
//    @ResponseBody

    @GetMapping("/{id}")  //相当于 @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public R findOne(@PathVariable("id") Integer id){
        System.out.println("findOne-id:" + id);

        User user = new User();
        user.setUname(id + "");
        return R.success(user);
    }

    /*
        http://localhost:80/rest/1
        请求方式为DELETE表示删除，结合路径，表示根据id删除用户
     */
//    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
//    @ResponseBody

    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") Integer id){
        System.out.println("del-id:" + id);

        return R.success("success");
    }

    /*
        http://localhost:80/rest
        请求方式为POST表示新增，结合路径，表示新增用户
     */
//    @RequestMapping(method = RequestMethod.POST)
//    @ResponseBody

    @PostMapping
    public R add(@RequestBody User user){
        System.out.println("add-user:" + user);

        return R.success("success");
    }

    /*
        http://localhost:80/rest
        请求方式为PUT表示更新，结合路径，表示更新用户
     */
//    @RequestMapping(method = RequestMethod.PUT)
//    @ResponseBody

    @PutMapping
    public R update(@RequestBody User user){
        System.out.println("update-user:" + user);

        int i = 1/0;

        return R.success("success");
    }

    /*
        http://localhost:80/rest
        请求方式为GET表示获取，结合路径，表示获取全部用户
     */
//    @RequestMapping(method = RequestMethod.GET)
//    @ResponseBody

    @GetMapping
    public R findAll(){
        System.out.println("findAll-user:");

        List<User> users = new ArrayList<>();
        User user = new User();
        user.setUname("zs");
        user.setAge(18);

        users.add(user);
        return R.success(users);
    }
}
